package com.mobileagri.backoffice.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mobileagri.backoffice.dao.MessageDAO;
import com.mobileagri.backoffice.model.Message;

/**
 * Contact Service
 * 
 * 
 * @author LOUVET Romain
 */
@Service
public class MessageService {

	@Autowired
	private MessageDAO messageDAO;

	/**
	 * Get all Message
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Message> getMessageList() {

		List<Message> listMessage = messageDAO.getAll();

		// Remove Password User
		for (Message m : listMessage) {
			if (null != m.getUserDest()) {
				m.getUserDest().setPass("****");
			}
			if (null != m.getUserEmet()) {
				m.getUserEmet().setPass("****");
			}
		}

		return messageDAO.getAll();
	}

	/**
	 * Get all contacts
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Message> getMessageList(int start, int limit) {
		return messageDAO.getAll(start, limit, "desc");
	}

	/**
	 * Create new Contact/Contacts
	 * 
	 * @param data
	 *            - json data from request
	 * @return created contacts
	 */
	// @Transactional
	// public List<Contact> create(Contact contact) {
	//
	// List<Contact> newContacts = new ArrayList<Contact>();
	//
	// newContacts.add(contactDAO.saveContact(contact));
	//
	// return newContacts;
	// }

	/**
	 * Update contact/contacts
	 * 
	 * @param data
	 *            - json data from request
	 * @return updated contacts
	 */
	@Transactional
	public List<Message> update(Message contact) {

		List<Message> returnMessages = new ArrayList<Message>();

		returnMessages.add((Message) messageDAO.save(contact));
		returnMessages.add((Message) messageDAO.save(contact));

		return returnMessages;
	}

	/**
	 * Delete contact/contacts
	 * 
	 * @param contact
	 *            - json data from request
	 */
	// @Transactional
	// public void delete(Contact contact) {
	//
	// contactDAO.deleteContact(contact.getId());
	// }

	/**
	 * Get total of Contacts from database. Need to set this value on ExtJS
	 * Store
	 * 
	 * @return
	 */
	public int getTotalMessages() {

		return messageDAO.getTotal();
	}

	// /**
	// * Spring use - DI
	// *
	// * @param contactDAO
	// */
	// @Autowired
	// public void setContactDAO(ContactDAO contactDAO) {
	// this.contactDAO = contactDAO;
	// }

}
